<?php
class Bluess_Modules_Loader extends Zend_Controller_Plugin_Abstract
{
	protected $_modules;
	
	public function __construct(array $modulesList) 
	{
		$this->_modules = $modulesList;
	}
	
	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
	{
		
		$module = $request->getModuleName();
		
		if (!isset($this->_modules[$module])) {
		 
			throw new Exception("Module does not exist!");
		}

		$bootstrapPath = $this->_modules[$module];
		
		$bootstrapFile = dirname($bootstrapPath) . '/Bootstrap.php';
        $class         = ucfirst($module) . '_Bootstrap';
        $application   = new Zend_Application(
        	APPLICATION_ENV,
    		APPLICATION_PATH . '/modules/' . $module . '/configs/module.ini'
		);  

        if (Zend_Loader::loadFile('Bootstrap.php', dirname($bootstrapPath)) 
        	&& class_exists($class)) {
            $bootstrap = new $class($application);
            $bootstrap->bootstrap();
        }
	}
	
	
	public function preDispatch(Zend_Controller_Request_Abstract $request)
	{
		
	 
		/* if (Zend_Auth::getInstance ()->hasIdentity ()) {
			if ('logout' == $this->getRequest ()->getActionName ()) {
				$auth = Zend_Auth::getInstance();
				$auth->clearIdentity();
				 
				$this->getRequest ()->setModuleName($this->getRequest()->getModuleName())
				->setControllerName("gestauth")
				->setActionName("logout")
				->setDispatched(false);
			}
		}else{
			
			if ('login' != $this->getRequest ()->getActionName () && 'creationcompte' != $this->getRequest ()->getActionName ()) {
					echo $this->getRequest()->getModuleName();
					$this->getRequest ()->setModuleName($this->getRequest()->getModuleName())
					->setControllerName("gestauth")
					->setActionName("login")
					->setDispatched(false);
		
			}
			 
		}*/
		 
	
	
	}



}